package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.course.CourseOrderVo;
import com.wanxi.education.model.vo.catalogue.*;
import com.wanxi.education.model.vo.course.*;
import com.wanxi.education.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/24 0024
 * @Version: 1.0
 */
@RestController("CourseBackController")
@RequestMapping("/back/course")
@Api(tags = "课程管理 （后台管理）")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation("添加课程和目录信息")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody CourseAddVo courseAddVo,HttpSession httpSession){
        if (courseAddVo.getWeight() == null){
            return CommonResult.failure("权重不能为空");
        }
        if (courseAddVo.getCcieId() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseAddVo.getTeacher() == null){
            return CommonResult.failure("老师名不能为空");
        }
        if (courseAddVo.getTitle() == null){
            return CommonResult.failure("课程标题不能为空");
        }
        if (courseAddVo.getPrice() == null){
            return CommonResult.failure("课程价格不能为空");
        }
        if (courseAddVo.getPicture() == null){
            return CommonResult.failure("图片不能为空");
        }
        if (courseAddVo.getIntroduce() == null){
            return CommonResult.failure("课程介绍不能为空");
        }
        if (courseAddVo.getCatalogueAddVos() == null){
            return CommonResult.failure("目录不能为空");
        }
        Long adminId = (Long)httpSession.getAttribute("loginId");
        courseService.add(courseAddVo,adminId);

        return CommonResult.success("成功");
    }

    @ApiOperation("更新课程状态信息")
    @PostMapping("/updateStatusById")
    public CommonResult<String> updateStatusById(@RequestBody CourseStatusVo courseStatusVo){
        if (courseStatusVo.getId() == null){
            return CommonResult.failure("课程id不为空");
        }

        if (courseStatusVo.getStatus() == null){
            return CommonResult.failure("课程状态为空");
        }
        if (!(courseStatusVo.getStatus()==1 || courseStatusVo.getStatus() == 0)){
            return CommonResult.failure("课程状态异常");
        }
        courseService.updateStatusById(courseStatusVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("更新课程基础信息")
    @PostMapping("/updateBasicById")
    public CommonResult<String> updateBasicById(@RequestBody CourseBasicVo courseBasicVo, HttpSession httpSession){
        if (courseBasicVo.getId() == null){
            return CommonResult.failure("课程id不能为空");
        }
        if (courseBasicVo.getCcieId() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseBasicVo.getPicture() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseBasicVo.getPrice() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseBasicVo.getTeacher() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseBasicVo.getTitle() == null){
            return CommonResult.failure("证书id不能为空");
        }
        if (courseBasicVo.getWeight() == null){
            return CommonResult.failure("证书id不能为空");
        }
        Long id = (Long)httpSession.getAttribute("loginId");
        courseBasicVo.setAdminId(id);
        courseService.updateBasicById(courseBasicVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("更新课程介绍信息")
    @PostMapping("/updateInfoById")
    public CommonResult<String> updateInfoById(@RequestBody CourseInfoVo courseInfoVo){
        if (courseInfoVo.getId() == null){
            return CommonResult.failure("课程id不能为空");
        }
        courseService.updateInfoById(courseInfoVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("更新课程目录信息")
    @PostMapping("/updateCatalogueById")
    public CommonResult<String> updateCatalogueById(@RequestBody CatalogueUpdateVo catalogueUpdateVo){
        if (catalogueUpdateVo.getId() == null){
            return CommonResult.failure("目录id不能为空");
        }
        if (catalogueUpdateVo.getPid() == null){
            return CommonResult.failure("父级id不能为空");
        }
        if (catalogueUpdateVo.getTitle() == null){
            return CommonResult.failure("目录标题不能为空");
        }
        courseService.updateCatalogueById(catalogueUpdateVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("更新课程目录位置")
    @PostMapping("/updateCatalogueOrderById")
    public CommonResult<String> updateCatalogueOrderById(@RequestBody CatalogueOrderVo catalogueOrderVo){
        if (ObjectUtils.isEmpty(catalogueOrderVo.getMoveDownId())){
            return CommonResult.failure("下移id不能为空");
        }
        if (ObjectUtils.isEmpty(catalogueOrderVo.getMoveUpId())){
            return CommonResult.failure("上移id不能为空");
        }
        boolean byId = courseService.updateCatalogueOrderById(catalogueOrderVo);
        if (byId){
            return CommonResult.success("成功");
        }
        return CommonResult.failure("id不是同级");
    }

    @ApiOperation("分页查询找到所有课程信息，包括模糊查询")
    @PostMapping("/findPage")
    public CommonResult<PageInfo<CourseVo>> findPage(@RequestBody CoursePageVo coursePageVo){
        PageInfo<CourseVo> pageInfo = courseService.findPage(coursePageVo);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("通过课程id查询课程信息")
    @GetMapping("/findCourseBasicById")
    public CommonResult<CourseBasicVo> findCourseBasicById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        CourseBasicVo courseBasicVo = courseService.findCourseBasicById(id);
        return CommonResult.success(courseBasicVo);
    }

    @ApiOperation("通过课程id查询课程介绍信息")
    @GetMapping("/findCourseInfoById")
    public CommonResult<CourseInfoVo> findCourseInfoById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        CourseInfoVo courseInfoVo = courseService.findCourseInfoById(id);
        return CommonResult.success(courseInfoVo);
    }

    @ApiOperation("通过课程id查询全部目录信息")
    @GetMapping("/findCatalogueAllById")
    public CommonResult<List<CatalogueAllVo>> findCatalogueAllById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        List<CatalogueAllVo> catalogueAllVoList = courseService.findCatalogueAllById(id);
        return CommonResult.success(catalogueAllVoList);
    }

    @ApiOperation("通过目录id查询目录信息")
    @GetMapping("/findCatalogueById")
    public CommonResult<CatalogueInfoVo> findCatalogueById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        CatalogueInfoVo catalogueInfoVo = courseService.findCatalogueById(id);
        return CommonResult.success(catalogueInfoVo);
    }

    @ApiOperation("通过id删除课程信息")
    @GetMapping("/deleteById")
    public CommonResult<String> deleteById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        courseService.deleteById(id);
        return CommonResult.success("成功");
    }

    @ApiOperation("查询所有推荐课程信息")
    @GetMapping("/findRecommends")
    public CommonResult<List<CourseRecommendVo>> findRecommends(){
        List<CourseRecommendVo> recommends = courseService.findRecommends();
        return CommonResult.success(recommends);
    }

    @ApiOperation("通过课程id添加推荐课程信息")
    @GetMapping("/addRecommend")
    public CommonResult<String> addRecommend(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        boolean recommend = courseService.addRecommend(id);
        if (recommend){
            return CommonResult.success("成功");
        }
       return CommonResult.failure("重复添加课程 或 课程数量已满");
    }

    @ApiOperation("通过课程id删除推荐课程信息")
    @GetMapping("/deleteRecommend")
    public CommonResult<String> deleteRecommend(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        courseService.deleteRecommend(id);
        return CommonResult.success("成功");
    }

    @ApiOperation("通过课程id更新推荐课程顺序")
    @PostMapping("/updateRecommendOrderById")
    public CommonResult<String> updateRecommendOrderById(@RequestBody CourseOrderVo courseOrderVo){
        if (courseOrderVo.getMoveDownId() == null||courseOrderVo.getMoveUpId()==null){
            return CommonResult.failure("id为全空或一个为空");
        }

        boolean byId = courseService.updateRecommendOrderById(courseOrderVo);
        if (byId){
            return CommonResult.success("成功");
        }
        return CommonResult.failure("更新失败，传入id不存在");
    }

    @ApiOperation("通过课程id添加课程目录")
    @PostMapping("/addCatalogueById")
    public CommonResult<String> addCatalogueById(@RequestBody CatalogueChildInfoAddVo catalogueChildInfoAddVo,HttpSession httpSession){
        if(catalogueChildInfoAddVo.getCourseId() == null){
            return CommonResult.failure("课程id不能为空");
        }
        if (catalogueChildInfoAddVo.getPid() == null){
            return CommonResult.failure("父级id不能为空");
        }
        if (catalogueChildInfoAddVo.getTitle() == null){
            return CommonResult.failure("目录名不能为空");
        }
        Long id = (Long)httpSession.getAttribute("loginId");
        catalogueChildInfoAddVo.setAdminId(id);
        courseService.addCatalogueById(catalogueChildInfoAddVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("通过目录id删除目录")
    @GetMapping("/deleteCatalogueById")
    public CommonResult<String> deleteCatalogueById(@RequestParam("id") Long id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.failure("id不能为空");
        }
        courseService.deleteCatalogueById(id);
       return CommonResult.success("成功");
    }

}
